package com.api.shopapi.mapper;

import com.api.shopapi.entity.Cashier;
import com.api.shopapi.entity.dto.CashierDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface CashierMapper {

    @Insert("insert into cashier " +
            "(" +
            "CashierCode," +
            "UserCode," +
            "BusinessCode," +
            "Name," +
            "Status," +
            "CreateTime," +
            "Gender" +
            ")values(" +
            "#{cashier.CashierCode}," +
            "#{cashier.UserCode}," +
            "#{cashier.BusinessCode}," +
            "#{cashier.Name}," +
            "#{cashier.Status}," +
            "#{cashier.CreateTime}," +
            "#{cashier.Gender}" +
            ")")
    int insert(@Param("cashier") Cashier cashier);

    @Update("update cashier set" +
            " Status=#{cashier.Status}," +
            " Name=#{cashier.Name}," +
            " Gender=#{cashier.Gender}" +
            " where CashierCode=#{cashier.CashierCode}")
    int update(@Param("cashier") Cashier cashier);

    @Select("select * from cashier where CashierCode=#{cashierCode} limit 1")
    Cashier getByCashierCode(@Param("cashierCode") String cashierCode);

    @Select("select * from cashier where UserCode=#{userCode} limit 1")
    Cashier getByUserCode(@Param("userCode") String userCode);


    @Select("<script>" +
            "select * from cashier c left join user u on c.UserCode = u.UserCode " +
            "where c.BusinessCode=#{businessCode} " +
            "<if test =' status !=-1 '> " +
            " and  u.Status = #{status} " +
            "</if>" +
            "<if test ='name !=\"\" '>" +
            " and c.Name=#{name} " +
            "</if>" +
            "order by u.CreateTime" +
            "</script>")
    List<CashierDTO> getPage(@Param("name")String name,@Param("status")int status,
                             @Param("businessCode")String businessCode);


    @Select("select * from cashier c left join user u on c.UserCode = u.UserCode " +
            "where c.BusinessCode=#{businessCode} and u.Status= 1 ")
    List<CashierDTO> getList(@Param("businessCode")String businessCode);





    /**
     * 商家查看收银员列表
     * */
    @Select("<script>" +
            "select CashierCode from cashier" +
            " where BusinessCode=#{businessCode} " +
            "<if test =' name !=\"\" '>" +
            " and Name=#{name} " +
            "</if>" +
            "</script>")
    List<Cashier> ListByBusinessCode(@Param("name")String name, @Param("businessCode")String businessCode);



    /**
     * 政府查看收银员列表
     * */
    @Select("<script>" +
            "select CashierCode from cashier" +
            " where 1=1 " +

            "<if test ='businessCode !=\"\" '>" +
            " and businessCode = #{businessCode} " +
            "</if>" +

            "<if test ='businessCode ==\"\" '>" +
            " and BusinessCode in (select BusinessCode from business where governmentCode =#{governmentCode}) " +
            "</if>" +

            "</script>")
    List<Cashier> ListByGovernmentCodeBusinessCode(@Param("governmentCode")String governmentCode,
                                                  @Param("businessCode")String businessCode);




}
